home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / COPTIMIZATIONS.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-26  |  10.6 KB  |  352 lines

  1. /*****************************************************************************
  2.   
  3.   Zope Public License (ZPL) Version 1.0
  4.   -------------------------------------
  5.   
  6.   Copyright (c) Digital Creations.  All rights reserved.
  7.   
  8.   This license has been certified as Open Source(tm).
  9.   
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions are
  12.   met:
  13.   
  14.   1. Redistributions in source code must retain the above copyright
  15.      notice, this list of conditions, and the following disclaimer.
  16.   
  17.   2. Redistributions in binary form must reproduce the above copyright
  18.      notice, this list of conditions, and the following disclaimer in
  19.      the documentation and/or other materials provided with the
  20.      distribution.
  21.   
  22.   3. Digital Creations requests that attribution be given to Zope
  23.      in any manner possible. Zope includes a "Powered by Zope"
  24.      button that is installed by default. While it is not a license
  25.      violation to remove this button, it is requested that the
  26.      attribution remain. A significant investment has been put
  27.      into Zope, and this effort will continue if the Zope community
  28.      continues to grow. This is one way to assure that growth.
  29.   
  30.   4. All advertising materials and documentation mentioning
  31.      features derived from or use of this software must display
  32.      the following acknowledgement:
  33.   
  34.        "This product includes software developed by Digital Creations
  35.        for use in the Z Object Publishing Environment
  36.        (http://www.zope.org/)."
  37.   
  38.      In the event that the product being advertised includes an
  39.      intact Zope distribution (with copyright and license included)
  40.      then this clause is waived.
  41.   
  42.   5. Names associated with Zope or Digital Creations must not be used to
  43.      endorse or promote products derived from this software without
  44.      prior written permission from Digital Creations.
  45.   
  46.   6. Modified redistributions of any form whatsoever must retain
  47.      the following acknowledgment:
  48.   
  49.        "This product includes software developed by Digital Creations
  50.        for use in the Z Object Publishing Environment
  51.        (http://www.zope.org/)."
  52.   
  53.      Intact (re-)distributions of any official Zope release do not
  54.      require an external acknowledgement.
  55.   
  56.   7. Modifications are encouraged but must be packaged separately as
  57.      patches to official Zope releases.  Distributions that do not
  58.      clearly separate the patches from the original work must be clearly
  59.      labeled as unofficial distributions.  Modifications which do not
  60.      carry the name Zope may be packaged in any form, as long as they
  61.      conform to all of the clauses above.
  62.   
  63.   
  64.   Disclaimer
  65.   
  66.     THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  67.     EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68.     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  69.     PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  70.     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  71.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  72.     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  73.     USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  74.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75.     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  76.     OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  77.     SUCH DAMAGE.
  78.   
  79.   
  80.   This software consists of contributions made by Digital Creations and
  81.   many individuals on behalf of Digital Creations.  Specific
  82.   attributions are listed in the accompanying credits file.
  83.   
  84.  ****************************************************************************/
  85. #include "Python.h"
  86. #include "cPersistence.h"
  87.  
  88. static void PyVar_Assign(PyObject **v, PyObject *e) { Py_XDECREF(*v); *v=e;}
  89. #define ASSIGN(V,E) PyVar_Assign(&(V),(E))
  90. #define UNLESS(E) if(!(E))
  91. #define UNLESS_ASSIGN(V,E) ASSIGN(V,E); UNLESS(V)
  92. #define OBJECT(O) ((PyObject*)(O))
  93.  
  94. static PyObject *py__p_oid, *py__p_jar, *py___getinitargs__, *py___module__;
  95. static PyObject *py_new_oid, *py___class__, *py___name__;
  96.  
  97. static PyObject *InvalidObjectReference;
  98.  
  99. typedef struct {
  100.   PyObject_HEAD
  101.   PyObject *jar, *stackup, *new_oid;
  102. } persistent_id;
  103.  
  104. staticforward PyTypeObject persistent_idType;
  105.  
  106. static persistent_id *
  107. newpersistent_id(PyObject *ignored, PyObject *args)
  108. {
  109.   persistent_id *self;
  110.   PyObject *jar, *stackup;
  111.  
  112.   UNLESS (PyArg_ParseTuple(args, "OO", &jar, &stackup)) return NULL;
  113.   UNLESS(self = PyObject_NEW(persistent_id, &persistent_idType)) return NULL;
  114.   Py_INCREF(jar);
  115.   self->jar=jar;
  116.   Py_INCREF(stackup);
  117.   self->stackup=stackup;
  118.   self->new_oid=NULL;
  119.   return self;
  120. }
  121.  
  122.  
  123. static void
  124. persistent_id_dealloc(persistent_id *self)
  125. {
  126.   Py_DECREF(self->jar);
  127.   Py_DECREF(self->stackup);
  128.   Py_XDECREF(self->new_oid);
  129.   PyMem_DEL(self);
  130. }
  131.  
  132. static PyObject *
  133. persistent_id_call(persistent_id *self, PyObject *args, PyObject *kwargs)
  134. {
  135.   PyObject *object, *oid, *jar=NULL, *r=NULL, *klass=NULL;
  136.  
  137.   /*
  138.   def persistent_id(object, self=self,stackup=stackup):
  139.   */
  140.   UNLESS (PyArg_ParseTuple(args, "O", &object)) return NULL;
  141.  
  142.   /*
  143.     if (not hasattr(object, '_p_oid') or
  144.         type(object) is ClassType): return None
  145.    */
  146.  
  147.  
  148.   /* Filter out most objects with low-level test. 
  149.      Yee ha! 
  150.      (Also get klass along the way.)
  151.   */
  152.   if (! PyExtensionClass_Check(object))
  153.     if (PyExtensionInstance_Check(object))
  154.       {
  155.     UNLESS (klass=PyObject_GetAttr(object, py___class__)) 
  156.       {
  157.         PyErr_Clear();
  158.         goto not_persistent;
  159.       }
  160.     UNLESS (
  161.         PyExtensionClass_Check(klass) &&
  162.         (((PyExtensionClass*)klass)->class_flags 
  163.          & PERSISTENT_TYPE_FLAG)
  164.         )
  165.       goto not_persistent;
  166.  
  167.       }
  168.     else 
  169.       goto not_persistent;
  170.  
  171.   UNLESS (oid=PyObject_GetAttr(object, py__p_oid)) 
  172.     {
  173.       PyErr_Clear();
  174.       goto not_persistent;
  175.     }
  176.  
  177.   /*
  178.       if oid is None or object._p_jar is not self:
  179.    */
  180.   if (oid != Py_None)
  181.     {
  182.       UNLESS (jar=PyObject_GetAttr(object, py__p_jar)) PyErr_Clear();
  183.       if (jar && jar != Py_None && jar != self->jar)
  184.     {
  185.       PyErr_SetString(InvalidObjectReference, 
  186.               "Attempt to store an object from a foreign "
  187.               "database connection");
  188.       return NULL;
  189.     }
  190.     }
  191.  
  192.   if (oid == Py_None || jar != self->jar)
  193.     {
  194.       /*
  195.           oid = self.new_oid()
  196.           object._p_jar=self
  197.           object._p_oid=oid
  198.           stackup(object)
  199.       */
  200.       UNLESS (self->new_oid ||
  201.           (self->new_oid=PyObject_GetAttr(self->jar, py_new_oid)))
  202.         goto err;
  203.       ASSIGN(oid, PyObject_CallObject(self->new_oid, NULL));
  204.       UNLESS (oid) goto null_oid;
  205.       if (PyObject_SetAttr(object, py__p_jar, self->jar) < 0) goto err;
  206.       if (PyObject_SetAttr(object, py__p_oid, oid) < 0) goto err;
  207.       UNLESS (r=PyTuple_New(1)) goto err;
  208.       PyTuple_SET_ITEM(r, 0, object);
  209.       Py_INCREF(object);
  210.       ASSIGN(r, PyObject_CallObject(self->stackup, r));
  211.       UNLESS (r) goto err;
  212.       Py_DECREF(r);
  213.     }
  214.  
  215.   /*
  216.       klass=object.__class__
  217.  
  218.       if klass is ExtensionKlass: return oid
  219.   */
  220.   
  221.   if (PyExtensionClass_Check(object)) goto return_oid;
  222.  
  223.   /*
  224.       if hasattr(klass, '__getinitargs__'): return oid
  225.   */
  226.  
  227.   if ((r=PyObject_GetAttr(klass, py___getinitargs__)))
  228.     {
  229.       Py_DECREF(r);
  230.       goto return_oid;
  231.     }
  232.   PyErr_Clear();
  233.  
  234.   /*
  235.       module=getattr(klass,'__module__','')
  236.       if module: klass=module, klass.__name__
  237.       else: return oid # degenerate 1.x ZClass case
  238.   */
  239.   UNLESS (jar=PyObject_GetAttr(klass, py___module__)) goto err;
  240.  
  241.   UNLESS (PyObject_IsTrue(jar)) goto return_oid;
  242.  
  243.   ASSIGN(klass, PyObject_GetAttr(klass, py___name__));
  244.   UNLESS (klass) goto err;
  245.  
  246.   UNLESS (r=PyTuple_New(2)) goto err;
  247.   PyTuple_SET_ITEM(r, 0, jar);
  248.   PyTuple_SET_ITEM(r, 1, klass);
  249.   klass=r;
  250.   jar=NULL;
  251.  
  252.   /*      
  253.       return oid, klass
  254.   */
  255.   UNLESS (r=PyTuple_New(2)) goto err;
  256.   PyTuple_SET_ITEM(r, 0, oid);
  257.   PyTuple_SET_ITEM(r, 1, klass);
  258.   return r;
  259.  
  260. not_persistent:
  261.   Py_INCREF(Py_None);
  262.   return Py_None;
  263.  
  264. err:
  265.   Py_DECREF(oid);
  266.   oid=NULL;
  267.  
  268. null_oid:
  269. return_oid:
  270.   Py_XDECREF(jar);
  271.   Py_XDECREF(klass);
  272.   return oid;
  273. }
  274.  
  275.  
  276. static PyTypeObject persistent_idType = {
  277.   PyObject_HEAD_INIT(NULL)
  278.   0,                /*ob_size*/
  279.   "persistent_id",            /*tp_name*/
  280.   sizeof(persistent_id),        /*tp_basicsize*/
  281.   0,                /*tp_itemsize*/
  282.   /* methods */
  283.   (destructor)persistent_id_dealloc,    /*tp_dealloc*/
  284.   (printfunc)0,    /*tp_print*/
  285.   (getattrfunc)0,        /*obsolete tp_getattr*/
  286.   (setattrfunc)0,        /*obsolete tp_setattr*/
  287.   (cmpfunc)0,    /*tp_compare*/
  288.   (reprfunc)0,        /*tp_repr*/
  289.   0,        /*tp_as_number*/
  290.   0,        /*tp_as_sequence*/
  291.   0,        /*tp_as_mapping*/
  292.   (hashfunc)0,        /*tp_hash*/
  293.   (ternaryfunc)persistent_id_call,    /*tp_call*/
  294.   (reprfunc)0,        /*tp_str*/
  295.   (getattrofunc)0,    /*tp_getattro*/
  296.   (setattrofunc)0,    /*tp_setattro*/
  297.   
  298.   /* Space for future expansion */
  299.   0L,0L,
  300.   "C implementation of the persistent_id function defined in Connection.py"
  301. };
  302.  
  303. /* End of code for persistent_id objects */
  304. /* -------------------------------------------------------- */
  305.  
  306.  
  307. /* List of methods defined in the module */
  308.  
  309. static struct PyMethodDef Module_Level__methods[] = {
  310.   {"new_persistent_id", (PyCFunction)newpersistent_id, METH_VARARGS,
  311.    "new_persistent_id(jar, stackup, new_oid)"
  312.    " -- get a new persistent_id function"},
  313.   {NULL, (PyCFunction)NULL, 0, NULL}        /* sentinel */
  314. };
  315.  
  316. void
  317. initcoptimizations()
  318. {
  319.   PyObject *m, *d;
  320.   char *rev="$Revision: 1.9 $";
  321.  
  322. #define make_string(S) if (! (py_ ## S=PyString_FromString(#S))) return
  323.   make_string(_p_oid);
  324.   make_string(_p_jar);
  325.   make_string(__getinitargs__);
  326.   make_string(__module__);
  327.   make_string(__class__);
  328.   make_string(__name__);
  329.   make_string(new_oid);
  330.             
  331.   /* Get InvalidObjectReference error */
  332.   UNLESS (m=PyString_FromString("POSException")) return;
  333.   ASSIGN(m, PyImport_Import(m));
  334.   UNLESS (m) return;
  335.   ASSIGN(m, PyObject_GetAttrString(m, "InvalidObjectReference"));
  336.   UNLESS (m) return;
  337.   InvalidObjectReference=m;
  338.  
  339.   UNLESS (ExtensionClassImported) return;
  340.  
  341.   m = Py_InitModule4("coptimizations", Module_Level__methods,
  342.              "C optimizations",
  343.              (PyObject*)NULL,PYTHON_API_VERSION);
  344.   d = PyModule_GetDict(m);
  345.  
  346.   persistent_idType.ob_type=&PyType_Type;
  347.   PyDict_SetItemString(d,"persistent_idType", OBJECT(&persistent_idType));
  348.  
  349.   PyDict_SetItemString(d, "__version__",
  350.                PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));  
  351. }
  352.